package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.ExcelExport2;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.bo.SysComplaintsBO;
import com.ztgf.sys.models.dto.SysComplaintsDTO;
import com.ztgf.sys.models.service.ISysComplaintsService;
import com.ztgf.sys.models.vo.SysComplaintsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 投诉意见表控制器
 *
 * @author YUE
 * @Date 2020-05-27 11:24:51
 */

@RestController
@RequestMapping("/cms/sysComplaints")
@Slf4j
@Api(value = "SysComplaintsController",tags = "投诉意见表接口")
public class SysComplaintsController {

    @DubboReference
    private ISysComplaintsService sysComplaintsService;

    /**
     * 处理内容
     * @param id
     * @param processStr 处理意见内容
     * @return
     */
    @ApiOperation(value = "处理投诉意见表",notes = "处理投诉意见表")
    @PostMapping("/processComplaints")
    public Result processComplaints(Long id,String processStr){
        if(null == id){
            return new Result(CodeEnum.FAIL_100002 );
        }
       return sysComplaintsService.processComplaints(id,processStr);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysComplaintsDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysComplaintsDTO dto = sysComplaintsService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "投诉意见表列表",notes = "投诉意见表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysComplaintsDTO>> queryPageList(SysComplaintsBO bo){
        Wrapper<SysComplaintsDTO> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotEmpty(bo.getStartDate())){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (StringUtils.isNotEmpty(bo.getEndDate())){
            wrapper.le("create_time", bo.getEndDate());
        }
        if(null != bo.getState()){
            wrapper.eq("state", bo.getState());
        }
        String shopName = bo.getShopName();
        if(StrUtils.isNotEmpty(shopName)){
            wrapper.like("shop_name", shopName);
        }
        Page<SysComplaintsDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysComplaintsService.selectPage(page, wrapper);

        return new Result<>(page);
    }


    @GetMapping(value = "/batchExportComplaintList/{ids}")
    @ApiOperation(value = "批量导出投诉建议", notes = "批量导出投诉建议")
    public void batchExportComplaintList(HttpServletResponse response, @PathVariable String ids) {
        //创建一个数组用于设置表头
        String[] attributeNames = new String[]{"投诉人店铺id", "投诉人公司名字", "内容", "结果", "状态"};
        //查询订单
        List<SysComplaintsVO> list = new ArrayList<>();
        if (StringUtils.isNotBlank(ids)) {
            String[] idList = ids.split(",");
            for (String id : idList) {
                SysComplaintsDTO dto = sysComplaintsService.selectById(id);
                if (dto != null) {
                    SysComplaintsVO sysComplaintsVO = new SysComplaintsVO();
                    sysComplaintsVO.setState(dto.getState() == 1?"处理":"未处理");
                    sysComplaintsVO.setContent(dto.getContent());
                    sysComplaintsVO.setResult(dto.getResult());
                    sysComplaintsVO.setShopId(dto.getShopId());
                    sysComplaintsVO.setShopName(dto.getShopName());
                    list.add(sysComplaintsVO);
                }
            }
        } else {
            Wrapper<SysComplaintsDTO> wrapper = new EntityWrapper<>();
            List<SysComplaintsDTO> dtoList = sysComplaintsService.selectList(wrapper);
            for (SysComplaintsDTO dto : dtoList) {
                SysComplaintsVO sysComplaintsVO = new SysComplaintsVO();
                sysComplaintsVO.setState(dto.getState() == 1?"处理":"未处理");
                sysComplaintsVO.setContent(dto.getContent());
                sysComplaintsVO.setResult(dto.getResult());
                sysComplaintsVO.setShopId(dto.getShopId());
                sysComplaintsVO.setShopName(dto.getShopName());
                list.add(sysComplaintsVO);
            }
        }
        ExcelExport2.export(response, list, attributeNames, "_投诉建议");
    }
}
